<?php

class BlockCounterHits {
	private $show_visit_today = false;
	private $show_visit_yestoday = false;
	private $show_this_month = false;
	private $show_this_year= false;
	private $show_total_visit = false;
	private $show_hits_today = false;
	private $show_total_hits = true;
	private $show_online = true;
	private $show_member_visit = true;
	function __construct(){
		
	}
	
	public function counter(){
		
		$_TABLE_NAME = 'tb_countor_hits';
		$db = Zend_Registry::get('connectDB');
		$ip      = $_SERVER['REMOTE_ADDR']; // Getting the user's computer IP
		$tanggal = date("y-m-d"); // Getting the current date
		$waktu  = time();
		$bln=date("m");
		$tgl=date("d");
		$blan=date("Y-m");
		$thn=date("Y");
		$tglk=$tgl-1;
		// Check your IP, whether the user has had access to today's
		$sql = "SELECT * FROM `". $_TABLE_NAME . "` WHERE ip='$ip'  AND tanggal='$tanggal'";
		$stmt = $db->query($sql);
		//$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$rows = $stmt->fetchAll();
		if(count($rows) == 0){
			
			$sql = "INSERT INTO `". $_TABLE_NAME . "`(ip, tanggal, hits, online) VALUES('$ip','$tanggal','1','$waktu')";
			$stmt = new Zend_Db_Statement_Pdo($db, $sql);
			$stmt->execute();
		}else{
			
			$sql = "UPDATE `". $_TABLE_NAME . "` SET hits=hits+1, online='$waktu' WHERE ip='$ip' AND tanggal='$tanggal'";
			$stmt = new Zend_Db_Statement_Pdo($db, $sql);
			$stmt->execute();
		}
		if($tglk=='1' | $tglk=='2' | $tglk=='3' | $tglk=='4' | $tglk=='5' | $tglk=='6' | $tglk=='7' | $tglk=='8' | $tglk=='9'){
			$sql = "SELECT * FROM `". $_TABLE_NAME . "` WHERE tanggal='$thn-$bln-0$tglk'";
		
		} else {
			$sql = "SELECT * FROM `". $_TABLE_NAME . "` WHERE tanggal='$thn-$bln-$tglk'";
		}
		$stmt = $db->query($sql);
		$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$kemarin1 = count($stmt->fetchAll());
			
		
		$sql = "SELECT * FROM `". $_TABLE_NAME . "` WHERE tanggal LIKE '%$blan%'";
		$stmt = $db->query($sql);
		$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$bulan1 = count($stmt->fetchAll());
		
		$sql = "SELECT * FROM `". $_TABLE_NAME . "` WHERE tanggal LIKE '%$thn%'";
		$stmt = $db->query($sql);
		$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$tahunini1 = count($stmt->fetchAll());
		
		$sql = "SELECT * FROM `". $_TABLE_NAME . "` WHERE tanggal LIKE '%$thn%'";
		$stmt = $db->query($sql);
		$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$tahunini1 = count($stmt->fetchAll());
		
		$sql = "SELECT * FROM `". $_TABLE_NAME . "` WHERE tanggal='$tanggal' GROUP BY ip";
		$stmt = $db->query($sql);
		$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$pengunjung = count($stmt->fetchAll());
			
			
		$sql = "SELECT COUNT(hits) FROM `". $_TABLE_NAME . "`";
		$stmt = $db->query($sql);
		$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$totalpengunjung = $stmt->fetch();
		$sql = "SELECT SUM(hits) as hitstoday FROM `". $_TABLE_NAME . "` WHERE tanggal='$tanggal' GROUP BY tanggal";
		$stmt = $db->query($sql);
		$hits = $stmt->fetch();

		$sql = "SELECT SUM(hits) FROM `". $_TABLE_NAME . "`";
		$stmt = $db->query($sql);
		$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$totalhits = $stmt->fetch();
			
		$sql = "SELECT COUNT(hits) FROM `". $_TABLE_NAME . "`";
		$stmt = $db->query($sql);
		$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$tothitsgbr = $stmt->fetch();
		$bataswaktu       = time() - 300;
			
		$sql = "SELECT * FROM `". $_TABLE_NAME . "` WHERE online > '$bataswaktu'";
		$stmt = $db->query($sql);
		$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$pengunjungonline = count($stmt->fetchAll());
		$xhtml = '';
		if($this->show_visit_today){
			$xhtml = '<div class="row-item">';		
			$xhtml .= '<div class="label-item">Visit Today</div>';
			$xhtml .= '<div class="num-item">'.$pengunjung.'</div>';
			$xhtml .= '<div class="clear"></div>';
			$xhtml .= '</div>';
		}
		
		if($this->show_visit_yestoday)
		{
			$xhtml .= '<div class="row-item">';
			$xhtml .= '<div class="label-item">Yesterday</div>';
			$xhtml .= '<div class="num-item">'.$kemarin1.'</div>';
			$xhtml .= '<div class="clear"></div>';
			$xhtml .= '</div>';
		}
		
		if($this->show_this_month)
		{
			$xhtml .= '<div class="row-item">';
			$xhtml .= '<div class="label-item">This Month</div>';
			$xhtml .= '<div class="num-item">'.$bulan1.'</div>';
			$xhtml .= '<div class="clear"></div>';
			$xhtml .= '</div>';
		}
		
		if($this->show_this_year)
		{
			$xhtml .= '<div class="row-item">';
			$xhtml .= '<div class="label-item">This year</div>';
			$xhtml .= '<div class="num-item">'.$tahunini1.'</div>';
			$xhtml .= '<div class="clear"></div>';
			$xhtml .= '</div>';
		}
		
		if($this->show_total_visit)
		{
			$xhtml .= '<div class="row-item">';
			$xhtml .= '<div class="label-item">Lượt Truy Cập</div>';
			$xhtml .= '<div class="num-item">'.$totalpengunjung[0].'</div>';
			$xhtml .= '<div class="clear"></div>';
			$xhtml .= '</div>';
		}
		
		if($this->show_hits_today)
		{
			$xhtml .= '<div class="row-item">';
			$xhtml .= '<div class="label-item">Hits to day</div>';
			$xhtml .= '<div class="num-item">'.$hits['hitstoday'].'</div>';
			$xhtml .= '<div class="clear"></div>';
			$xhtml .= '</div>';
		}
		
		if($this->show_total_hits)
		{
			$xhtml .= '<div class="row-item">';
			$xhtml .= '<div class="label-item">Lượt Truy Cập</div>';
			$xhtml .= '<div class="num-item">'.$totalhits[0].'</div>';
			$xhtml .= '<div class="clear"></div>';
			$xhtml .= '</div>';
		}
		
		if($this->show_online)
		{
			$xhtml .= '<div class="row-item">';
			$xhtml .= '<div class="label-item">Trực Tuyến</div>';
			$xhtml .= '<div class="num-item">'.$pengunjungonline.'</div>';
			$xhtml .= '<div class="clear"></div>';
			$xhtml .= '</div>';
		}
		return $xhtml;
	}
	
	public function counterMember(){
		$db = Zend_Registry::get('connectDB');
		$_TABLE_NAME = 'tb_users';
		$sql = "SELECT COUNT(id) FROM `". $_TABLE_NAME . "` WHERE kich_hoat = 1";
		$stmt = $db->query($sql);
		$stmt->setFetchMode(Zend_Db::FETCH_NUM);
		$users = $stmt->fetch();
		$xhtml = '<div class="row-item end-row">';
		$xhtml .= '<div class="label-item">Thành Viên</div>';
		$xhtml .= '<div class="num-item">'.$users[0].'</div>';
		$xhtml .= '<div class="clear"></div>';
		$xhtml .= '</div>';

		return $xhtml;
	}
}
